package com.ronglian.ucdisk.web;

import java.beans.PropertyEditorSupport;
import java.rmi.server.ServerCloneException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;



import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ronglian.ucdisk.share.ShareEvent;
import com.ronglian.ucdisk.share.service.ShareService;

@RequestMapping("/**")
@Controller
public class ShareController  {
	
	@Autowired
	private ShareService service;
	
	public static String SENDER_ID = "Share_Test_Sender";
	@RequestMapping("/create") 
	public @ResponseBody MessageBean create(
			String token, 
			@RequestParam(required=true)String nodeIdList,
			@RequestParam(required=false,defaultValue=",")String receiverIdList,
			@RequestParam(required=false,defaultValue=",")String recTeamIdList,
			@RequestParam(required=true)String shareTitle,
			@RequestParam(required=false)Date endTime,
			@RequestParam(required=true)String type,
			@RequestParam(required=true)String permission){
		MessageBean msg = new MessageBean();
		ShareEvent event = new ShareEvent();
		event.id = UUID.randomUUID().toString();
		//event.senderId = UUID.randomUUID().toString();
		event.senderId  = SENDER_ID ;
		event.shareTitle = shareTitle;
		event.endTime = endTime;
		event.setNodeIdList( nodeIdList) ;
		event.setReceiverIdList(  receiverIdList ) ; 
		event.setRecTeamIdList(recTeamIdList);
		event.type = type ; 
		event.permission = permission;
		
		event.createTime = new Date() ;
		event.lastUpdateTime = new Date(); 
		if("url".equals(type)) {
			event.code = "todo: testcode";
			event.url ="http://testurl";
		}
		service.createShare(event);
		msg.putBody("shareEvent", event);
		msg.fireSuccess();
		return msg;
	}
	@RequestMapping("/myshares") 
	public @ResponseBody MessageBean myShares(
			String token , 
			@RequestParam(required=true)int pageSize, 
			@RequestParam(required=true)int pageNumber) 
	{
		MessageBean msg = new MessageBean();
		msg.fireSuccess();
		long total = service.mySharesTotalCount(SENDER_ID);
		List<ShareEvent> events = new ArrayList<ShareEvent>() ; 
		if(total >= pageSize * (pageNumber -1 )) {
			events =  service.myShares(SENDER_ID, pageSize, pageNumber);
		}
		msg.putBody("total", total);	
		msg.putBody("current", events.size());
		msg.putBody("shareList", events);
		return msg;
	}
	
	@RequestMapping("/sharestome")
	public @ResponseBody MessageBean sharesToMe(
			String token , 
			@RequestParam(required=true)int pageSize, 
			@RequestParam(required=true)int pageNumber) 
	{
		MessageBean msg = new MessageBean();
		msg.fireSuccess();
		long total = service.sharesToMeTotalCount(SENDER_ID) ;
		
		List<ShareEvent> events = new ArrayList<ShareEvent>() ; 
		if(total >= pageSize * (pageNumber -1 )) {
			events =  service.sharesToMe(SENDER_ID, pageSize, pageNumber);
		}
		
		msg.putBody("total", total);
		msg.putBody("current", events.size());
		msg.putBody("shareList", events);
		return msg;
	}
	
	@RequestMapping("/info")
	public @ResponseBody MessageBean viewInfo(String token,
			@RequestParam(required=true)String shareEventId){
		MessageBean msg = new MessageBean();
		msg.fireSuccess();
		ShareEvent event =service.getShareDetail(shareEventId) ; 
		msg.putBody("shareEvent", event);
		return msg;
	}
	
	
	@InitBinder  
	public void InitBinder(WebDataBinder dataBinder)  
	{  
	    dataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {  
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	    	SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			@Override
			public String getAsText() {
				if (this.getValue() == null) {
					return "";
				}
				return this.sdf.format((Date) this.getValue());
			}

			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				if (null == text || text.equals("")) {
					return;
				}
				try {
					if(text.length() > 15 ){
						this.setValue(this.sdfTime.parse(text));
					}else{
						this.setValue(this.sdf.parse(text));
					}
				} catch (Exception e) {
					this.setValue(new Date());
				}
			}
	    });
	    dataBinder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {  
	    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

			@Override
			public String getAsText() {
				if (this.getValue() == null) {
					return "";
				}
				return this.sdf.format((Date) this.getValue());
			}

			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				if (null == text || text.equals("")) {
					return;
				}
				try {
					this.setValue(this.sdf.parse(text));
				} catch (Exception e) {
					this.setValue(new Date());
				}
			}
	    });
	    
	    dataBinder.registerCustomEditor(int.class, new PropertyEditorSupport() {  


			@Override
			public void setAsText(String text) throws IllegalArgumentException {
				if (null == text || text.equals("")) {
					this.setValue(0);
				}else{
					this.setValue(Integer.valueOf(text));
				}
			}
	    });
	}
}
